<?php
/**
 * 销售统计管理控制器
 */
class StatisticsController extends App_Zend_Controller_Action_Admin
{
	/**该方法主要进行的是权限判断*/
	public function init()
	{
		$user = App_User::getCurrentUser();
		if($user){
			if(!in_array('3', json_decode($user->permission))){
				exit('You have no priviage');
			}
		}else{
			$this->_redirect('/user/login');
		}
		parent::init();
	}
	
	//每日销售统计
	public function edayAction()
	{
        $orderlog = App_Model::factory('OrderLog');
        $log = App_Model::factory('StockLog');
        
        $starttime = $this->getRequest()->getParam('starttime',date('Y-m-d',strtotime('-1 day')));
        $endtime = date('Y-m-d',(strtotime($starttime)+60*60*24));
        $site = $this->getRequest()->getParam('site','');
        
        $siteArr = array('gogoalshop.com','minejerseys.com','jerseysmark.com','goaljerseys.com','fancybestdeal.com','elmontyouthsoccer.com','bestcheapsoccer.com');
        $timeArr = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24');
        $data = array();
        $sum = array();
        $total = array();
        if(empty($site)){
        	foreach ($siteArr as $sitev){
        		$ol = $orderlog->getStaOrderLog($sitev,$starttime,$endtime)->toArray();
        		$sl = $log->getStaStockLog($sitev,$starttime,$endtime)->toArray();
        		$stime = strtotime($starttime);
        		foreach ($ol as $olv){
        			foreach ($timeArr as $hour){
        				if($olv['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($hour)-1)*60*60) && $olv['add_time'] < date('Y-m-d H:i:s',($stime+intval($hour)*60*60))){
        					$data[substr($sitev,0,-4)]['money'][$hour] += $olv['total_amount'];
        					$data[substr($sitev,0,-4)]['order'][$hour] += 1;
        				}
        			}
        		}
        		foreach ($sl as $slv){
        			foreach ($timeArr as $hour){
        			    if($slv['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($hour)-1)*60*60) && $slv['add_time'] < date('Y-m-d H:i:s',($stime+intval($hour)*60*60))){
        				    foreach (json_decode($slv['info']) as $infov){
        				    	$data[substr($sitev,0,-4)]['piece'][$hour] += $infov[1];
        				    }
        			    }
        			}
        		}
        	}
        	
        	//统计成符合饼状图的数据
        	foreach ($data as $site2=>$d){
        		foreach ($d as $type=>$dd){
        			foreach ($dd as $per){
        				$sum[$type][$site2] += $per;
        				$total[$type] += $per;
        			}
        		}
        	}
        }else{
        	$ol = $orderlog->getStaOrderLog($site,$starttime,$endtime)->toArray();
        	$sl = $log->getStaStockLog($site,$starttime,$endtime)->toArray();
        	$stime = strtotime($starttime);
        	foreach ($ol as $olv2){
        		foreach ($timeArr as $hour){
        			if($olv2['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($hour)-1)*60*60) && $olv2['add_time'] < date('Y-m-d H:i:s',($stime+intval($hour)*60*60))){
        				$data[substr($site,0,-4)]['money'][$hour] += $olv2['total_amount'];
        				$data[substr($site,0,-4)]['order'][$hour] += 1;
        			}
        		}
        	}
        	foreach ($sl as $slv2){
        		foreach ($timeArr as $hour){
        			if($slv2['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($hour)-1)*60*60) && $slv2['add_time'] < date('Y-m-d H:i:s',($stime+intval($hour)*60*60))){
        				foreach (json_decode($slv2['info']) as $infov){
        					$data[substr($site,0,-4)]['piece'][$hour] += $infov[1];
        				}
        			}
        		}
        	}
        }
        
        $this->view->siteArr = $siteArr;
        $this->view->sum = $sum;
        $this->view->total = $total;
        $this->view->data = $data;
        $this->view->site = $site;
        $this->view->endtime = $endtime;
        $this->view->starttime = $starttime;
	}
	
	//每周销售统计
	public function eweekAction()
	{
        $orderlog = App_Model::factory('OrderLog');
        $log = App_Model::factory('StockLog');
        
        $endtime = $this->getRequest()->getParam('endtime',date('Y-m-d',strtotime('-1 day')));
        $starttime = $this->getRequest()->getParam('starttime',date('Y-m-d',(strtotime($endtime)-60*60*24*7)));
        $site = $this->getRequest()->getParam('site','');
        
        $siteArr = array('gogoalshop.com','minejerseys.com','jerseysmark.com','goaljerseys.com','fancybestdeal.com','elmontyouthsoccer.com','bestcheapsoccer.com');
        $timeArr = array();
        for($i = 1;$i<=(strtotime($endtime)-strtotime($starttime))/(60*60*24);$i++){
        	$timeArr[] = $i;
        }
        $data = array();
        $sum = array();
        $total = array();
        if(empty($site)){
        	foreach ($siteArr as $sitev){
        		$ol = $orderlog->getStaOrderLog($sitev,$starttime,$endtime)->toArray();
        		$sl = $log->getStaStockLog($sitev,$starttime,$endtime)->toArray();
        		$stime = strtotime($starttime);
        		foreach ($ol as $olv){
        			foreach ($timeArr as $day){
        				if($olv['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($day)-1)*60*60*24) && $olv['add_time'] < date('Y-m-d H:i:s',($stime+intval($day)*60*60*24))){
        					$data[substr($sitev,0,-4)]['money'][$day] += $olv['total_amount'];
        					$data[substr($sitev,0,-4)]['order'][$day] += 1;
        				}
        			}
        		}
        		foreach ($sl as $slv){
        			foreach ($timeArr as $day){
        			    if($slv['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($day)-1)*60*60*24) && $slv['add_time'] < date('Y-m-d H:i:s',($stime+intval($day)*60*60*24))){
        				    foreach (json_decode($slv['info']) as $infov){
        				    	$data[substr($sitev,0,-4)]['piece'][$day] += $infov[1];
        				    }
        			    }
        			}
        		}
        	}
        	
        	//统计成符合饼状图的数据
        	foreach ($data as $site2=>$d){
        		foreach ($d as $type=>$dd){
        			foreach ($dd as $per){
        				$sum[$type][$site2] += $per;
        				$total[$type] += $per;
        			}
        		}
        	}
        }else{
        	$ol = $orderlog->getStaOrderLog($site,$starttime,$endtime)->toArray();
        	$sl = $log->getStaStockLog($site,$starttime,$endtime)->toArray();
        	$stime = strtotime($starttime);
        	foreach ($ol as $olv2){
        		foreach ($timeArr as $day){
        			if($olv2['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($day)-1)*60*60*24) && $olv2['add_time'] < date('Y-m-d H:i:s',($stime+intval($day)*60*60*24))){
        				$data[substr($site,0,-4)]['money'][$day] += $olv2['total_amount'];
        				$data[substr($site,0,-4)]['order'][$day] += 1;
        			}
        		}
        	}
        	foreach ($sl as $slv2){
        		foreach ($timeArr as $day){
        			if($slv2['add_time'] >= date('Y-m-d H:i:s', $stime+(intval($day)-1)*60*60*24) && $slv2['add_time'] < date('Y-m-d H:i:s',($stime+intval($day)*60*60*24))){
        				foreach (json_decode($slv2['info']) as $infov){
        					$data[substr($site,0,-4)]['piece'][$day] += $infov[1];
        				}
        			}
        		}
        	}
        }
        
        $this->view->siteArr = $siteArr;
        $this->view->sum = $sum;
        $this->view->total = $total;
        $this->view->data = $data;
        $this->view->site = $site;
        $this->view->endtime = $endtime;
        $this->view->starttime = $starttime;
	}
	
	//每月销售统计
	public function emonthAction()
	{
	
	}
	
	//季度销售统计
	public function equarterAction()
	{
	
	}

	//全年销售统计
	public function eyearAction()
	{
	
	}
}
?>